package com.bss.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andy.basics.MessageHandler;
import com.andy.basics.Service;
import com.bss.model.Admission;
import com.bss.model.User;
import com.bss.service.UserService;

@Controller
@Scope("request")
public class UserController extends com.andy.basics.Controller<User> {

	@Autowired
	private UserService userService;

	private String password;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return this.userService;
	}

	public String getUserName() {
		return this.userService.getUserName();
	}

	public Admission createAdmission() {
		return new Admission();
	}

	@Override
	public User create() {
		String name = this.getHttpUtil().getRemoteUser();
		if (name != null) {
			List<User> u = this.userService.find("username='" + name + "'");
			if ((u != null) && (u.size() > 0)) {
				System.out.println("Password is " + u.get(0).getPassword());
				this.setPassword(u.get(0).getPassword());
				return u.get(0);
			}
		}
		return new User();
	}

	@Override
	public Service<User> getService() {
		return this.userService;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return this.password;
	}

	@Override
	protected boolean validate(User model, MessageHandler handler) {
		if (!("" + this.getPassword()).equals(model.getPassword())) {
			handler.addMessage("Password", "Passwords must match !");
		}
		return this.getValidator().validate(model, handler);
	}
}
